import request from "@/utils/request";

export interface DispatchQueryParams {
  keyword?: string;
  orderNumber?: string;
  receiver?: string;
  status?: string;
  destination?: string;
  departureSite?: string;
  arrivalSiteId?: number;
  dispatchStartDate?: string;
  dispatchEndDate?: string;
  current?: number;
  size?: number;
}

export interface DispatchConfirmParams {
  dispatchId: number;
  actualDepartureTime?: string;
  remark?: string;
}

export interface DispatchArrivalParams {
  dispatchId: number;
  actualArrivalTime?: string;
  remark?: string;
}

/**
 * 获取发车列表
 */
export const getDispatchList = (params: DispatchQueryParams) => {
  return request.post("/api/tenant/dispatch/list", params);
};

/**
 * 创建发车记录
 */
export const createDispatch = (
  vehicleId: number,
  destinationSite: string,
  remark?: string
) => {
  return request.post("/api/tenant/dispatch/create", null, {
    params: { vehicleId, destinationSite, remark },
  });
};

/**
 * 创建临时车辆发车记录
 */
export const createTempDispatch = (
  vehiclePlate: string,
  vehicleType: string,
  destinationSite: string,
  driverName: string,
  driverPhone?: string,
  remark?: string
) => {
  return request.post("/api/tenant/dispatch/create-temp", null, {
    params: {
      vehiclePlate,
      vehicleType,
      destination: destinationSite,
      driverName,
      driverPhone,
      remark,
    },
  });
};

/**
 * 确认发车
 */
export const confirmDispatch = (params: DispatchConfirmParams) => {
  return request.post("/api/tenant/dispatch/confirm", params);
};

/**
 * 确认到达
 */
export const confirmArrival = (params: DispatchArrivalParams) => {
  return request.post("/api/tenant/dispatch/arrive", params);
};

/**
 * 删除发车记录（物理删除，仅待发车状态）
 */
export const deleteDispatch = (dispatchId: number) => {
  return request.delete(`/api/tenant/dispatch/delete/${dispatchId}`);
};

/**
 * 取消发车记录（状态变更，仅已发车状态）
 */
export const cancelDispatch = (dispatchId: number, cancelReason: string) => {
  return request.post(`/api/tenant/dispatch/cancel/${dispatchId}`, null, {
    params: { cancelReason },
  });
};

/**
 * 获取发车记录详情
 */
export const getDispatchById = (dispatchId: number) => {
  return request.get(`/api/tenant/dispatch/${dispatchId}`);
};

/**
 * 获取车辆订单列表
 */
export const getVehicleOrders = (vehicleId: number) => {
  return request.get(`/api/tenant/dispatch/vehicle/${vehicleId}/orders`);
};
